package com.web22_2_1.servlet;

import com.web22_2_1.entity.Course;
import com.web22_2_1.entity.Student;
import com.web22_2_1.entity.vo.GradeVO;
import com.web22_2_1.service.CourseService;
import com.web22_2_1.service.StudentService;
import com.web22_2_1.service.impl.CourseServiceImpl;
import com.web22_2_1.service.impl.GradeServiceImpl;
import com.web22_2_1.service.impl.StudentServiceImpl;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import com.web22_2_1.service.GradeService;

import java.io.IOException;
import java.sql.Date;
import java.util.List;

@WebServlet("/grade/*")
public class GradeServlet extends HttpServlet {
    private CourseService courseService;
    private StudentService studentService;
    private GradeService gradeService;

    @Override
    public void init() {
        gradeService = new GradeServiceImpl();
        studentService = new StudentServiceImpl();
        courseService = new CourseServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求的路径信息
        String pathInfo = req.getPathInfo();
        RequestDispatcher dispatcher;
        List<String> stuNames;
        List<String> courseNames;

        // 打印请求相关路径信息，用于调试和日志记录
        System.out.println("请求路径：" + req.getRequestURL());
        System.out.println("上下文路径：" + req.getContextPath());
        System.out.println("Servlet路径：" + req.getServletPath());
        System.out.println("具体子路径：" + pathInfo);

        // 根据路径信息执行不同的操作
        switch (pathInfo) {
            case "/list":
                // 列出所有成绩
                List<GradeVO> gradeList = gradeService.listAll();
                req.setAttribute("gradeList", gradeList);
                dispatcher = req.getRequestDispatcher("/grade.jsp"); // 使用 forward 方法
                dispatcher.forward(req, resp);
                break;
            case "/select":
                // 处理选择操作
                break;
            case "/add":
                // 准备添加成绩，提供学生和课程列表
                req.setAttribute("grade", null);
                stuNames = studentService.getAllStudents().stream().map(Student::getName).toList();
                courseNames = courseService.getAllCourses().stream().map(Course::getCourseName).toList();
                req.setAttribute("stuNames", stuNames);
                req.setAttribute("courseNames", courseNames);
                dispatcher = req.getRequestDispatcher("/gradeForm.jsp");
                dispatcher.forward(req, resp);
                break;
            case "/insert":
                // 处理添加成绩操作
                String studentName = req.getParameter("studentName");
                String courseName = req.getParameter("courseName");
                Float score = Float.parseFloat(req.getParameter("score"));
                String term = req.getParameter("term");
                Date examDate = Date.valueOf(req.getParameter("examDate"));
                gradeService.addGrade(new GradeVO(studentName, courseName, score, term, examDate));
                resp.sendRedirect(req.getContextPath() + "/grade/list");
                break;
            case "/del":
                // 处理删除成绩操作
                int id1 =Integer.parseInt(req.getParameter("gradeId"));
                if (gradeService.deleteGrade(id1)) {
                    resp.sendRedirect(req.getContextPath() + "/grade/list");
                } else {
                    resp.sendRedirect(req.getContextPath() + "/Error.jsp");
                }
                break;
            case "/edit":
                // 准备编辑成绩，提供当前成绩信息及学生和课程列表
                int id = Integer.parseInt(req.getParameter("gradeId"));
                GradeVO cur = gradeService.getGradeById(id);
                stuNames = studentService.getAllStudents().stream().map(Student::getName).toList();
                courseNames = courseService.getAllCourses().stream().map(Course::getCourseName).toList();
                dispatcher = req.getRequestDispatcher("/gradeForm.jsp");
                req.setAttribute("grade", cur);
                req.setAttribute("stuNames", stuNames);
                req.setAttribute("courseNames", courseNames);
                dispatcher.forward(req, resp);
                break;
            case "/update":
                // 处理更新成绩操作
                String gradeId = req.getParameter("gradeId");
                studentName = req.getParameter("studentName");
                courseName = req.getParameter("courseName");
                score = Float.parseFloat(req.getParameter("score"));
                term = req.getParameter("term");
                examDate = Date.valueOf(req.getParameter("examDate"));
                GradeVO temp = new GradeVO(studentName,courseName,score,term,examDate);
                temp.setGradeId(Integer.parseInt(gradeId));
                gradeService.updateGrade(temp);

                resp.sendRedirect(req.getContextPath() + "/grade/list");
                break;
            default:
                // 处理未知路径
                System.out.println("未知路径：" + pathInfo);
                resp.sendRedirect(req.getContextPath() + "/Error.jsp"); // 确保路径正确
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}